using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using AppScan.Scan.Results;
using System.IO;
using AppScan.Diagnostics;

namespace AppScanNameSpace.WordReporter
{
	public partial class WordReporterForm : Form
	{
		/// <summary>
		/// ref to the extension obj
		/// </summary>
		private WordReporterExtension _extension;

		/// <summary>
		/// CTor
		/// </summary>
		/// <param name="extension">the extension object</param>
		public WordReporterForm(WordReporterExtension extension)
		{
			_extension = extension;

			InitializeComponent();

			tbFileName.Text = _extension.DefaultFile;
		}

		private void btnPickFile_Click(object sender, EventArgs e)
		{
			docFileDialog.FileName = tbFileName.Text;
			DialogResult res = docFileDialog.ShowDialog(this);
			if (res == DialogResult.OK)
			{
				tbFileName.Text = docFileDialog.FileName;
			}
		}



		private void btnGenerateReport_Click(object sender, EventArgs e)
		{
			WordMerger merger = new WordMerger(listStatus);
            if (_extension.CanRunReports())
            {
				if (merger.Merge(_extension.AppScan, tbFileName.Text, chkShowWord.Checked))
                {
                    // success, save the filename to the registry
					_extension.SaveSettings(tbFileName.Text);
                }
            }
		}

		private void linkFieldRef_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
		{
		
			string file = "";
			try
			{
				// TBD this doesn't work as the assembly is copied by word
				file = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) 
						+ Path.DirectorySeparatorChar
						+ "WordReporter.html";
				System.Diagnostics.Process runIE = new System.Diagnostics.Process();
				runIE.StartInfo.RedirectStandardOutput = false;
				runIE.StartInfo.FileName = file;
				runIE.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;
				runIE.StartInfo.UseShellExecute = true;
				runIE.Start();
			}
			catch (Exception ex)
			{
                Debug.Logger.Log(ex.ToString());
				MessageBox.Show("Could not load help file " + file + Environment.NewLine + ex.ToString());
				return;
			}
		}
	}
}